<?php

include_once('./config/config.php');
include_once('./config/check.php');

  $id = empty($_GET['id']) ? '' : $_GET['id'];

  //查询这个用户名是否存在，防止他验证后再次提交
  $sql = "SELECT * FROM {$pre_}person WHERE `id` = '$id'";
  $admin = find($sql);
  $num1 = $admin['depid'];
  $num2 = $admin['jobid'];
  // $sql1 = "SELECT d.name as d_name,j.name as j_name FROM {$pre_}departments as d INNER JOIN {$pre_}jobs as j ON  d.id = j.depid where d.id = $num1 and j.id = $num2";
  // $admin1 = find($sql1);
  $sql1 = "SELECT * FROM {$pre_}departments WHERE `id` = '$num1'";
  $admin1 = find($sql1);

  $sql2 = "SELECT * FROM {$pre_}jobs WHERE `id` = '$num2'";
  $admin2 = find($sql2);

  // var_dump($num);
  // exit;
  if (!empty($_POST)) {
  $username = empty($_POST['username']) ? '' : $_POST['username'];
  // 查询这个用户名是否存在，防止他验证后再次提交
  $sql = "SELECT * FROM {$pre_}jobs WHERE `name` = '$username'";
  $user = find($sql);
  if ($user) {
      ShowMsg('用户已存在，请重新输入');
      exit;
  }

  //电话
  $mobile = isset($_POST['mobile']) ? trim($_POST['mobile']) : '';
  $m_sql = "SELECT * FROM {$pre_}person WHERE `mobile` = '$mobile'";
  $mobile_sql = find($m_sql);
    // 更改电话判断
    // if($mobile_sql){
    //   ShowMsg('电话已存在，请检查');
    //   exit;
    // }
  $email = isset($_POST['email']) ? trim($_POST['email']) : '';
  $e_sql = "SELECT * FROM {$pre_}person WHERE `email` = '$email'";
  $email_sql = find($e_sql);
    // 更改邮箱判断
    // if($email_sql){
    //   ShowMsg('邮箱已存在，请检查');
    //   exit;
    // }
  // $sex = isset($_POST['sex']) ? trim($_POST['sex']) : '';
    // // 更改判断
    // if(empty($sex)){
    //   ShowMsg('性别不能为空');
    //   exit;
    // }
  $department = isset($_POST['department']) ? trim($_POST['department']) : '';
    // 更改部门判断
    if(empty($department)){
      ShowMsg('部门不能为空');
      exit;
    }
  $job = isset($_POST['job']) ? trim($_POST['job']) : '';
    // 更改职位判断
    if(empty($job)){
      ShowMsg('职位不能为空');
      exit;
    }

    //组装数据
    $data = [
      // 'name' => $username, //员工名字不用修改
      'mobile' => $mobile,
      'email' => $email,
      // 'sex' => $sex,
      'depid' => $department,
      'jobid' => $job,
      // 'avatar' =>$result,  
    ];


  //先判断是否有文件上传 (错误=0=没错误  大小>0)
  if ($_FILES['avatar']['error'] == 0 && $_FILES['avatar']['size'] > 0) {

    $result = upload('avatar', 'assets/uploads/');

    if ($result['success']) {

     //删除原先的头像
      @is_file($admin['avatar']) && @unlink($admin['avatar']);
      $data['avatar'] = $result['msg'];
      

    } else {

      ShowMsg($data['msg']);
      exit;
    }
  }


  //插入到数据中
  $insert = update('person', $data, 'id='.$id);
  
  if($insert) {
    ShowMsg('修改用户成功','user.php');
    exit;
  } else{
    ShowMsg('修改用户失败');
    exit;
  }
}
?>



<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
</head>

<body>


  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">发布文章</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.html">Home</a> <span class="divider">/</span></li>
      <li class="active">Index</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 员工列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>用户名</label>
                <input type="text" name="username" class="input-xxlarge" value="<?php echo $admin['name'];?>"  disabled>
                <label>电话</label>
                <input type="tel" name="mobile" value="<?php echo $admin['mobile'];?>" class="input-xxlarge">
                <label>邮箱</label>
                <input type="email" name="email" value="<?php echo $admin['email'];?>" class="input-xxlarge">
                <label>性别</label>
                <input type="text" name="sex" value="<?php if($admin['sex'] == 0){echo "男";}else if($admin['sex'] == 1){echo "女";}else{echo "保密";}?>" class="input-xxlarge" disabled>
                <label>部门</label>
                <input type="text" name="department" value="<?php echo $admin1['name'];?>" class="input-xxlarge">
                <label>职位</label>
                <input type="text" name="job" value="<?php echo $admin2['name'];?>" class="input-xxlarge">
                <label>头像</label>
                <input type="file" name="avatar" class="input-xxlarge" />
                <label></label>
                <input id="add" class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
  $("input[name='username']").change(function() {

    //用户名
    var username = $(this).val()


    //JS版本

    //创建异步对象
    var ajax = new XMLHttpRequest();


    //绑定请求状态  当请求被发送到服务器时，我们需要执行一些基于响应的任务
    ajax.onreadystatechange = function() {
      // console.log(ajax.readyState)
      //http的状态码
      // console.log(ajax.status)
      // if(ajax.readyState == 4)
      if (ajax.status == 200) {
        //获取到的是一个 json的字符串
        // console.log(ajax.responseText)

        //将获取到的json 变成js的数据类型
        var result = JSON.parse(ajax.responseText)

        //根据结果来判断
        if (result.success) {
          //可以注册
          $("#notice").css({
            'display': 'inline-block',
            'color': 'green'
          })
          $("#notice").html(result.msg)
          $("#add").prop('disabled', false)
        } else {
          //不可以注册
          $("#notice").css({
            'display': 'inline-block',
            'color': 'red'
          })
          $("#notice").html(result.msg)
          $("#add").prop('disabled', true)
        }
      }
    }


    //发起请求  false 同步  true 异步
    ajax.open('post', 'AdminAdd.php?action=check', true)

    //post的请求一定要设置 http头信息
    ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

    //发送请求，并传递数据
    ajax.send(`username=${username}`)
  })
</script>